package com.engine.common.baseEntity.jsonResponse;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.time.LocalDateTime;

@Slf4j
@Data
public class JsonResponseEntity implements Serializable {

    @JSONField(ordinal = 1)
    private Boolean success;
    @JSONField(ordinal = 2)
    private Integer code;
    @JSONField(ordinal = 3)
    private String message;
    @JSONField(ordinal = 4)
    private Object result;
    private Integer page;
    private LocalDateTime timestamp;
    @JSONField(serialize=false)
    private HttpServletResponse response;

    public JsonResponseEntity(){

    }

    public JsonResponseEntity(Boolean success, Integer code, String message, Object result, Integer page, HttpServletResponse response) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.result = result;
        this.page = page;
        this.timestamp = LocalDateTime.now();
        response.setStatus(code);
        if (!success) {
            response.setStatus(code);
        }
    }
    public JsonResponseEntity(Boolean success, Integer code, String message, HttpServletResponse response){
        this.success = success;
        this.code = code;
        this.message = message;
        this.timestamp = LocalDateTime.now();
        response.setStatus(code);
        if (!success) {
            response.setStatus(code);
        }
    }

    @JSONField(serialize=false)
    public static String getJsonResponseString(Boolean success, Integer code, String message, Object result, Integer page, HttpServletResponse response)
    {
        JsonResponseEntity jsonResponseEntity=new JsonResponseEntity(success, code, message,result,page, response);
        return jsonResponseEntity.getJsonString(SerializerFeature.WriteMapNullValue);
    }

    @JSONField(serialize=false)
    public static String getJsonResponseString(Boolean success, Integer code, String message, HttpServletResponse response)
    {
        JsonResponseEntity jsonResponseEntity=new JsonResponseEntity(success, code, message, response);
        return jsonResponseEntity.getJsonString();
    }


    @JSONField(serialize=false)
    public static byte[] getJsonResponseByte(Boolean success, Integer code, String message,  HttpServletResponse response)
    {
        JsonResponseEntity jsonResponseEntity=new JsonResponseEntity(success, code, message, response);
        byte[] b;
        try{
            b=jsonResponseEntity.getJsonString().getBytes("utf-8");
        }
        catch (Exception e)
        {
            b=jsonResponseEntity.getJsonString().getBytes();
        }
        return b;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }

    @JSONField(serialize=false)
    public String getJsonString()
    {
        return JSONObject.toJSON(this).toString();
    }

    @JSONField(serialize=false)
    public String getJsonString(SerializerFeature serializerFeature)
    {
        return JSONObject.toJSONString(JSONObject.toJSON(this), serializerFeature);
    }
}
